package com.xxwu.springlibrary.service;

import com.xxwu.springlibrary.enums.BookStatus;
import com.xxwu.springlibrary.mapper.BookMapper;
import com.xxwu.springlibrary.model.BookInfo;
import com.xxwu.springlibrary.model.PageRequest;
import com.xxwu.springlibrary.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Service
public class BookService {
    //取对象
    @Autowired
    private BookMapper bookMapper;
    //获取图书列表
    public PageResult<BookInfo> getBookList(PageRequest pageRequest) {
        //获取图书总数
        Integer count = bookMapper.getBookCount();
        //查询当前页数据
        List<BookInfo> bookInfoList = bookMapper.getBookList(pageRequest);
        for(BookInfo bookInfo : bookInfoList) {
            if(bookInfo.getStatus() == 1){
                bookInfo.setStateCN("可借阅");
            } else if(bookInfo.getStatus() == 0) {
                bookInfo.setStateCN("无效");
            } else {
                bookInfo.setStateCN("不可借阅");
            }
        }
        return new PageResult<>(count, bookInfoList, pageRequest);
    }
    //添加图书信息
    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insert(bookInfo);
    }

    //获取图书列表
    public PageResult<BookInfo> getList(PageRequest pageRequest) {
        //获取图书总数
        Integer count = bookMapper.getBookCount();
        //查询当前页数据
        List<BookInfo> bookInfoList = bookMapper.getBookList(pageRequest);
        //通过枚举类对设置图书属性进行封装
        for(BookInfo bookInfo : bookInfoList) {
            bookInfo.setStateCN(BookStatus.getStatuCN(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(count, bookInfoList, pageRequest);
    }

    public BookInfo getBookById(Integer bookId) {
        return bookMapper.getBookById(bookId);
    }

    public Integer updateBooks(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(@RequestParam List<Integer> ids) {
        return bookMapper.updateBatchBook(ids);
    }
}
